package com.douyuehan.doubao.wechatpay.model.vo;

import lombok.Data;

import java.io.Serializable;

@Data
public class MessageTo<T> implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -7124170122394232970L;

    public final static String DATA_ABORT = "数据异常";
    public final static String PARAMETER_INVALID = "参数异常";

    /***
     * 标志位 0操作成功，非0 失败！
     */
    private int success;

    /**
     * 状态码
     */
    private int code;

    /***
     * 描述信息，可以提供给用户
     */
    private String message;

    /***
     * 记录总量
     */
    private long total;

    /***
     * 附加的数据
     */
    private T data;

    //附加信息
    private Object tag;

    public MessageTo() {
        this(0, "");
    }

    public MessageTo(T t) {
        this(0, "");
        data = t;
    }

    public MessageTo(int success, String desc) {
        this.success = success;
        this.message = desc;
    }

    public MessageTo success(String desc){
        this.message = desc;
        this.success = 0;
        return this;
    }

    public MessageTo fail(String desc){
        this.message = desc;
        this.success = 1;
        return this;
    }

    public MessageTo fail(int status, String desc){
        this.message = desc;
        this.success = status;
        return this;
    }

    public MessageTo setData(T data) {
        this.data = data;
        return this;
    }

    public MessageTo setData(T data, int code) {
        this.data = data;
        this.code = code;
        return this;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("成功：%s, 描述 ：%s, Data:%s", success, message, data == null ? "null" : data.toString());
    }

    public static MessageTo genExceptionResult(String msg, int code) {

        MessageTo msgTo = new MessageTo();
        msgTo.setMessage(msg);
        msgTo.setCode(code);
        return msgTo;
    }



}
